App.MainApp.module('leafletModule', function(leafletModule){
	var _map = null;
	var _baselayer = null;
	// View for Layout
   var MainView = Marionette.ItemView.extend({
   });
	this.startWithParent = false;
	// Controller for Layout
	var Controller = Marionette.Controller.extend({
		initialize: function(options){
			if (App.debug) console.log('App.leafletModule Controller initialized');
			this.region = options.region
		},
		show: function(options) {
			if (App.debug) console.log('App.leafletModule Controller show invoked');
			var main_view = new MainView({
				id: options.id,
				template: options.template
			});
			this.region.show(main_view);
		},
		render: function(options) {
			if (App.debug) console.log('App.leafletModule Controller show invoked');
			this.set_map(options.lon, options.lat, options.zoom);
			_baselayer = L.tileLayer('http://{s}.tile.cloudmade.com/8ee2a50541944fb9bcedded5165f09d9/997/256/{z}/{x}/{y}.png', {
				attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
				maxZoom: 18
			}).addTo(_map);
		},
		set_map: function(lon, lat, zoom) {
			_map = L.map('main-map-container').setView([lon, lat], zoom);
			_map.trackResize = false;
		},
		redraw: function() {
			if (App.debug) console.log('App.leafletModule Controller redraw invoked');
			L.Util.requestAnimFrame(_map.invalidateSize, _map, false, _map._container)
		}
	});
	
	leafletModule.addInitializer(function (options) {
		var defaults = {
			id: 'main-map-container',
			template: '#map-layout-template',
			region: App.MainApp.mapRegion,
			lon: 51.505,
			lat: -0.09,
			zoom: 13
		};
		options = _.extend(defaults, options);
		if (App.debug) console.log('App.leafletModule addInitializer');
		leafletModule.Controller = new Controller({
			region: options.region
		});
		leafletModule.Controller.show(options);
		leafletModule.Controller.render(options);
   });
});

App.MainApp.leafletModule.on("before:start", function(){
	if (App.debug) console.log('App.leafletModule before:start');
});

App.MainApp.leafletModule.on("start", function(){
	if (App.debug) console.log('App.leafletModule start');
	
});

